<template>
  <div class="app-container">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="区域名称" prop="areaName">
          <el-input
            v-model="queryParams.areaName"
            placeholder="请输入区域名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="区域类型" prop="levelType">
          <el-select v-model="queryParams.levelType" placeholder="请选择区域类型" clearable>
            <el-option
              v-for="dict in dict.type.level_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['region:zone:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['region:zone:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="zoneList" row-key="id" border :height="getAvailableHeight"
                default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <!--      <el-table-column label="平台表单自增主键" align="center" prop="id"/>-->
        <!--      <el-table-column label="租户ID" align="center" prop="tenantId"/>-->
        <el-table-column label="区域名称" align="left" prop="areaName"/>
        <el-table-column label="分区编号" align="center" prop="areaId" width="150"/>
        <el-table-column label="区域类型" align="center" prop="levelType" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.level_type" :value="scope.row.levelType"/>
          </template>
        </el-table-column>
        <!--      <el-table-column label="父级区域" align="center" prop="pid"/>-->
        <el-table-column label="排序号" align="center" prop="rank" width="100"/>
        <el-table-column label="区域描述" align="center" prop="details"/>
        <!--      <el-table-column label="分区点集合" align="center" prop="AreaPoint"/>-->
        <el-table-column label="缩放级别" align="center" prop="zoom" width="100"/>
        <el-table-column label="备注" align="center" prop="remark"/>
        <!--      <el-table-column label="是否删除" align="center" prop="deleteFlag"/>-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleAdd(scope.row)"
              v-hasPermi="['region:zone:edit']"
            >添加下级
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['region:zone:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['region:zone:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改区域管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <!--        <el-form-item label="租户ID" prop="tenantId">-->
          <!--          <el-input v-model="form.tenantId" placeholder="请输入租户ID"/>-->
          <!--        </el-form-item>-->
          <!--        <el-form-item label="区域ID" prop="AreaID">-->
          <!--          <el-input v-model="form.AreaID" placeholder="请输入区域ID"/>-->
          <!--        </el-form-item>-->
          <el-form-item label="父级区域" prop="pid">
            <!--          <el-input v-model="form.pid" placeholder="请输入父级区域"/>-->
            <treeselect v-model="form.pid" :options="parentZoneOptions" :normalizer="normalizer"
                        placeholder="请选择父级区域"/>
          </el-form-item>
          <el-form-item label="区域名称" prop="areaName">
            <el-input v-model="form.areaName" placeholder="请输入区域名称"/>
          </el-form-item>
          <el-form-item label="区域类型" prop="levelType">
            <el-select v-model="form.levelType" placeholder="请选择区域类型" style="width: 100%;">
              <el-option
                v-for="dict in dict.type.level_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序号" prop="rank">
            <el-input-number v-model="form.rank" :min="1" placeholder="请输入排序号" style="width: 100%;"/>
          </el-form-item>
          <el-form-item label="区域描述" prop="details">
            <el-input v-model="form.details" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="缩放级别" prop="zoom">
            <el-input-number v-model="form.zoom" :min="1" :max="19" placeholder="请输入缩放区域" style="width: 100%;"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
          <!--        <el-form-item label="分区点集合" prop="AreaPoint">-->
          <!--          <el-input v-model="form.AreaPoint" type="textarea" placeholder="请输入内容"/>-->
          <!--        </el-form-item>-->

          <!--        <el-form-item label="是否删除" prop="deleteFlag">-->
          <!--          <el-input v-model="form.deleteFlag" placeholder="请输入是否删除"/>-->
          <!--        </el-form-item>-->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </ContentWrap>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listZone, getZone, delZone, addZone, updateZone} from "@/api/leakage/zone";

export default {
  name: "Zone",
  components: {Treeselect},
  dicts: ['level_type'],
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 区域管理表格数据
      zoneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        levelType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // tenantId: [
        //   {required: true, message: "租户ID不能为空", trigger: "blur"}
        // ],
        // AreaID: [
        //   {required: true, message: "区域ID不能为空", trigger: "blur"}
        // ],
        areaName: [
          {required: true, message: "区域名称不能为空", trigger: "blur"}
        ],
        levelType: [
          {required: true, message: "区域类型不能为空", trigger: "blur"}
        ],
        // pid: [
        //   {required: true, message: "父级区域不能为空", trigger: "blur"}
        // ],
        // deleteFlag: [
        //   {required: true, message: "是否删除不能为空", trigger: "blur"}
        // ]
      },
      parentZoneOptions: []
    };
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 184
    },
  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询区域管理列表 */
    getList() {
      this.loading = true;
      listZone(this.queryParams).then(response => {
        this.zoneList = response;
        this.parentZoneOptions = JSON.parse(JSON.stringify(response));
        this.parentZoneOptions.forEach(zone => this.resetTreeNodeId(zone))
        this.loading = false;
      });
    },
    resetTreeNodeId(zone) {
      zone.id = zone.areaId
      if (zone.children && zone.children.length) {
        zone.children.forEach(child => this.resetTreeNodeId(child))
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        AreaID: null,
        areaName: null,
        pid: null,
        details: null,
        remark: null,
        rank: null,
        levelType: null,
        Type: null,
        AreaPoint: null,
        zoom: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row) {
        this.form.pid = row.id
      }
      this.open = true;
      this.title = "添加区域管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getZone(id).then(response => {
        this.form = response.data;
        this.form.pid = (response.data.pid === 'root' ? undefined : response.data.pid)
        this.open = true;
        this.title = "修改区域管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateZone(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZone(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal.confirm('是否确认删除区域管理编号为"' + IDs + '"的数据项？').then(function () {
        return delZone(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('region/zone/export', {
        ...this.queryParams
      }, `区域信息.xlsx`)
    }
  }
};
</script>
